package com.example.fw;

import java.util.List;
import java.util.Random;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

import com.example.tests.ContactData;
import com.example.utils.SortedListOf;

public class ContactHelper extends HelperBase {
	
	public static boolean CREATION = true;
	public static boolean MODIFICATION = false;

	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}
	
	private SortedListOf<ContactData> cachedContacts;
	
	public SortedListOf<ContactData> getContacts() {
		if(cachedContacts == null) {
			rebuildCacheContacts();
		}
		return cachedContacts;
	}
	
	private void rebuildCacheContacts() {
		cachedContacts = new SortedListOf<ContactData>();
		manager.navigateTo().MainPage();
		List<WebElement> lines = driver.findElements(By.xpath("//tr[@name='entry']"));
        for (WebElement line : lines)
        {
                List<WebElement> cells = line.findElements(By.tagName("td"));
                WebElement tdFirstName = cells.get(2);
                WebElement tdLastName = cells.get(1);  
                String firstname = tdFirstName.getText();
                String lastname = tdLastName.getText();
                cachedContacts.add(new ContactData().withFirstname(firstname).withLastname(lastname));
       } 
	}
	
	public ContactHelper createContact(ContactData contact) {
		manager.navigateTo().MainPage();
		initContactCreation();
		fillContactForm(contact, CREATION);
		submitContactCreation();
		returnToHomePage();
		rebuildCacheContacts();
		return this;
	}
	
	public ContactHelper modifyContact(int index, ContactData contact) {
		manager.navigateTo().MainPage();
		initContactEdit(index);
    	fillContactForm(contact, MODIFICATION);
    	submitContactModification();
    	returnToHomePage();
    	rebuildCacheContacts();
		return this;
	}
	
	public ContactHelper deleteContactForm(int index) {
		manager.navigateTo().MainPage();
		initContactEdit(index);
		submitContactDeletion();
    	returnToHomePage();
    	rebuildCacheContacts();
		return this;
	}

	//-----------------------------------------------------------------------------

	public ContactHelper fillContactForm(ContactData contact, boolean formType) {
		type(By.name("firstname"), contact.getFirstname());
		type(By.name("lastname"), contact.getLastname());	
		type(By.name("address"), contact.getAddress());
		type(By.name("home"), contact.getHometelephone());
	  	type(By.name("mobile"), contact.getMobiletelephone());
		type(By.name("work"), contact.getWorktelephone());
		type(By.name("email"), contact.getEmail());
	 	type(By.name("email2"), contact.getEmail2());
	    contact.withBday(selectRandomValue("bday"));
	    contact.withBmonth(selectRandomValue("bmonth"));
	    type(By.name("byear"), contact.getByear());
	    type(By.name("address2"), contact.getAddress2());
	    type(By.name("phone2"), contact.getTelephone2());
	    
	    if (formType == CREATION) {
	    	
	    } else {
	    	if (driver.findElements(By.name("new_group")).size() !=0) {
	    		throw new Error("Group selector exists in modification form");
	    	}
	    }
	    return this;
	}

    public String selectRandomValue (String type) {
        Select dropdown = new Select(chooseDropDown(type));
        Random rnd = new Random();
        int listSize = dropdown.getOptions().size();
        int randomIndex = rnd.nextInt(listSize);
        dropdown.selectByIndex(randomIndex);
        return dropdown.getAllSelectedOptions().get(0).getText();
}


    public WebElement chooseDropDown (String type) {
        WebElement selectElem;
        if (type == "bday") {
                selectElem = driver.findElement(By.name("bday"));
        } else {
                if (type == "bmonth") {
                        selectElem = driver.findElement(By.name("bmonth"));
                } else { 
                        selectElem = driver.findElement(By.name("new_group"));
                        }
                }
                return selectElem;
    }
	
	public ContactHelper initContactCreation() {
		click(By.linkText("add new"));
		return this;
	}
	
	public ContactHelper submitContactCreation() {
		click(By.name("submit"));
		cachedContacts = null;
		return this;
	}

	public ContactHelper returnToHomePage() {
		click(By.linkText("home page"));
		return this;
	}
	
	public ContactHelper submitContactModification() {
        click(By.xpath("(//input[@name='update'])"));
        cachedContacts = null;
        return this;
	}

	public ContactHelper initContactEdit(int index) {
		index++;
        click(By.xpath("//table[@id='maintable']/tbody/tr[" + index + "]/td[7]/a/img"));
        return this;
	}

	public void submitContactDeletion() {
		click(By.xpath("//*[@id='content']/form[2]/input[2]"));
		cachedContacts = null;
	}
	
}
